package com.suse.common.util.core;

import com.alibaba.fastjson2.JSONObject;
import com.suse.common.util.funcation.Converter;

import java.util.ArrayList;
import java.util.List;

public class BeanUtil extends cn.hutool.core.bean.BeanUtil{

    public static <T,R> List<R> copyToList(List<T> ts, Class<R> type, Converter<T,R> converter) {
        List<R> rs = new ArrayList<>();
        for (T t : ts) {
            R r = cn.hutool.core.bean.BeanUtil.copyProperties(t, type);
            converter.convert(t,r);
            rs.add(r);
        }
        return rs;
    }

    public static <T,R> R copyProperties(T t, Class<R> type, Converter<T,R> converter) {
        R r = cn.hutool.core.bean.BeanUtil.copyProperties(t, type);
        converter.convert(t,r);
        return r;
    }

    public static <T> T jsonToBean(String json,Class<T> type) {
        return JSONObject.parseObject(json, type);
    }

    public static String beanToJson(Object bean) {
        return JSONObject.toJSONString(bean);
    }

}